package com.eascs.spider.zhaopin.util;

import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class PropertiesUtil {

	private static Properties zhaopin = new Properties();
	private static Properties area = new Properties();
	private static Properties shenghui = new Properties();
	private static Map<String, String> nameMap = new HashMap<>();
	static{
		try {
			//FileInputStream is1 = new FileInputStream("src/main/resources/zhaopin.properties");
		    FileInputStream is1 = new FileInputStream("zhaopin.properties");
			zhaopin.load(is1);
			is1.close();
			
			//FileInputStream is2 = new FileInputStream("src/main/resources/area.properties");
			FileInputStream is2 = new FileInputStream("area.properties");
			area.load(is2);
			is2.close();
			
			//FileInputStream is3 = new FileInputStream("src/main/resources/shenghui.properties");
			FileInputStream is3 = new FileInputStream("shenghui.properties");
			shenghui.load(is3);
			is3.close();
			initNameMap();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//=================================招聘==============================
	private static String getZhaopinString(String key){
		return zhaopin.getProperty(key);
	}
	
	public static String get51jobCookie(){
		return getZhaopinString("51job_cookie");
	}
	
	public static String getZhilianCookie(){
		return getZhaopinString("zhilian_cookie");
	}
	
	//=================================招聘==============================
	
	
	
	//=================================省份编码==============================
	public static String getAreaString(String key) throws UnsupportedEncodingException{
		return area.getProperty(new String(key.getBytes("utf-8") , "ISO-8859-1"));
	}
	//=================================省份编码==============================
	
	
	
	//=================================省会城市编码==============================
	public static String getShenghuiString(String key) throws UnsupportedEncodingException{
		return shenghui.getProperty(new String(key.getBytes("utf-8") , "ISO-8859-1"));
	}
	
	public static List<String> getShenghui() throws UnsupportedEncodingException{
		List<String> list = new ArrayList<>();
		for(Object key : shenghui.keySet()){
			//String kv = new String(((String)key).getBytes("ISO-8859-1")) +"|"+ shenghui.getProperty((String)key);
			String kv = shenghui.getProperty((String)key);
			list.add(kv);
		}
		return list;
		
	}
	
	public static void initNameMap(){
		for(Object key : area.keySet()){
			nameMap.put(area.getProperty((String)key), (String)key);
		}
		for(Object key : shenghui.keySet()){
			nameMap.put(shenghui.getProperty((String)key), (String)key);
		}
	}
	
	//=================================省会城市编码==============================
	
	//根据省份或城市编码获取省份名字或城市名字
	public static String getNameByCode(String name) throws UnsupportedEncodingException{
		return new String(nameMap.get(name).getBytes("ISO-8859-1") ,"utf-8");
	}
	
	public static void main(String[] args) throws Exception{
		System.out.println(getNameByCode("160200"));
	}
}
